<template>
  <div class="process-route-management">
    <el-card style="margin-bottom: 20px">
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="工艺路线编号">
          <el-input v-model="searchForm.procedureCode" placeholder="请输入工艺路线编号"/>
        </el-form-item>
        <el-form-item label="工艺路线名称">
          <el-input v-model="searchForm.procedureName" placeholder="请输入工艺路线名称"/>
        </el-form-item>
        <el-form-item label="是否启用">
          <el-select v-model="searchForm.isEnabled" placeholder="请输入是否启用" style="width: 150px">
            <el-option label="是" value="0"/>
            <el-option label="否" value="1"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch">搜索</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <div class="table-operations">
        <el-button type="primary" @click="onAdd" plain>新增</el-button>
        <el-button type="success" @click="handleEdit" plain>修改</el-button>
        <el-button type="danger" @click="handleDelete" plain>删除</el-button>
      </div>

      <el-table :data="tableData" ref="multipleTable" style="width: 100%" @selection-change="handleSelectionChange" max-height="380px">
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="procedureCode" label="工艺路线编号"/>
        <el-table-column prop="procedureName" label="工艺路线名称"/>
        <el-table-column prop="description" label="工艺路线说明"/>
        <el-table-column prop="isEnabled" label="是否启用">
          <template #default="scope">
            <el-tag :type="scope.row.isEnabled =='0' ? 'success' : 'danger'">
              {{ scope.row.isEnabled == '0' ? '是' : '否' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注"/>
        <el-table-column label="操作" width="150">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)">修改</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :page-sizes="[10, 20, 30, 40]"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <addProcess :addDialog="addDialog" @close-dialog="closeDialog"/>
    <updateProcess :updateDialog="updateDialog" :id="select_orderId" @close-dialog="closeDialog"/>
  </div>
</template>

<script setup>
import {onMounted, reactive, ref} from 'vue'
import {ElMessage, ElMessageBox} from 'element-plus'
import {deleteProcess, findProcess} from "@/apis/zyj/Process.js";
import addProcess from "@/components/Process/AddDialog.vue"
import updateProcess from "@/components/Process/UpdateDialog.vue"

const searchForm = reactive({
  procedureCode: '',
  procedureName: '',
  isEnabled: ''
})

const tableData = reactive([])

onMounted(() => {
  initData()
})

function initData() {
  findProcess(searchForm, currentPage.value, pageSize.value).then(res => {
    console.log("数据", res)
    tableData.length = 0
    Object.assign(tableData, res.records)
    total.value = res.total
  })
}

const addDialog = ref(false)
const updateDialog = ref(false)

const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(7)

const select_order_number = ref('') //表格select选中的条数
const select_orderId = ref([]) //表格select复选框选中的id
const multipleSelection = ref()//选中行的数据
const multipleTable = ref()//用于表格多选框选中的清理

const onSearch = () => {
  initData()
}

const onReset = () => {

  searchForm.procedureCode = ''
  searchForm.procedureName = ''
  searchForm.isEnabled = ''
}

const onAdd = () => {
  addDialog.value = true
}

const onDelete = () => {
  ElMessage.warning('执行删除操作')
}

const onExport = () => {
  ElMessage.success('执行导出操作')
}

const handleEdit = (row) => {
  if (row.procedureCode != null) {
    select_orderId.value = row.procedureCode
  } else {
    select_orderId.value = multipleSelection.value[0].procedureCode
  }
  updateDialog.value = true
}

const handleDelete = (row) => {
  if (row.procedureCode != null) {
    select_order_number.value = "1";
  } else {
    select_order_number.value = multipleSelection.value.length;
  }
  ElMessageBox.confirm(
      '是否要删除 ' + select_order_number.value + '条数据',
      '警告',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(() => {
        if (row.procedureCode != null) {
          select_orderId.value = [];
          select_orderId.value.push(row.procedureCode)
        }
        console.log(row, select_orderId.value)
        deleteProcess(select_orderId.value).then((res) => {
          if (res == '成功') {
            initData()
            ElMessage.success('成功删除: ' + select_order_number.value + '条数据')
            select_order_number.value = '0';
            select_orderId.value = [];
            multipleTable.value.clearSelection();
          } else {
            ElMessage.error('系统错误！删除失败')
          }
        })
      })
      .catch(() => {
        ElMessage.info('删除取消')
      })
}

const handleSizeChange = (val) => {
  initData()
}

const handleCurrentChange = (val) => {
  initData()
}
//选中事件
const handleSelectionChange = (val) => {
  // 解决来回切换页面，也无法清除上次选中情况
  multipleSelection.value = val;
  select_order_number.value = multipleSelection.value.length;
  select_orderId.value = [];
  if (val) {
    val.forEach((row) => {
      if (row) {
        select_orderId.value.push(row.procedureCode);
      }
    });
  }
}

const closeDialog = () => {
  addDialog.value = false
  updateDialog.value = false
  initData()
}
</script>

<style scoped>
.process-route-management {
  padding: 20px;
}

.table-operations {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  text-align: right;
}

.el-pagination {
  justify-content: flex-end;
}
</style>